<template>
  <div class="app-container">

    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="标的编号">
        <el-input v-model="pageDto.search" placeholder="标的编号" />
      </el-form-item>

      <el-button icon="el-icon-search" type="primary" @click="fetchData()">
        查询
      </el-button>
    </el-form>

    <el-table
      :data="tableData.records"
      border
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="编号"
        prop="id"
        width="50"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="标的编号"
        prop="lendNo"
        width="200"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="投标金额"
        prop="amount"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="投资期数"
        prop="period"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="年化利率"
        prop="lendYearRate"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="已投资金额"
        prop="investAmount"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="投资人数"
        prop="investNum"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="发布时间"
        prop="publishDate"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="开始时间"
        prop="lendStartDate"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="结束时间"
        prop="lendEndDate"
      >
      </el-table-column>

      <el-table-column
        align="center"
        label="状态"
        prop="status"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="mini" type="warning">

          </el-tag>
          <el-tag
            v-else-if="scope.row.status === 1"
            size="mini"
            type="warning"
          >
            募资中
          </el-tag>

          <el-tag
            v-else-if="scope.row.status === 2"
            size="mini"
          >
            还款中
          </el-tag>

          <el-tag
            v-else-if="scope.row.status === 3"
            size="mini"
            type="success"
          >
            已结清
          </el-tag>

          <el-tag
            v-else-if="scope.row.status === 4"
            size="mini"
            type="success"
          >
            结标
          </el-tag>

          <el-tag
            v-else-if="scope.row.status === -1"
            size="mini"
            type="danger"
          >
            撤标
          </el-tag>
        </template>

      </el-table-column>

      <el-table-column
        align="center"
        fixed="right"
        label="操作"
        width="100"
      >
        <template slot-scope="scope">

          <router-link
            :to="'/core/lend/detail/'+scope.row.id"
            style="margin-right: 5px"
          >
            <el-button size="small" type="text">查看</el-button>
          </router-link>

          <el-button size="small" type="text" @click="makeLoan(scope.row.id)">放款</el-button>
        </template>
      </el-table-column>


    </el-table>

    <el-pagination
      :current-page="tableData.current"
      :page-size="tableData.size"
      :page-sizes="[2, 5, 10]"
      :total="tableData.total"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 10px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>

  </div>
</template>

<script>
import lendApi from '@/api/core/lend'

export default {
  name: 'List',
  data() {
    return {
      tableData: [],
      pageDto: {
        pageIndex: 1,
        pageSize: 2,
        search: ''
      }
    }
  },
  methods: {
    fetchData() {
      lendApi.page(this.pageDto)
        .then(response => {
          this.tableData = response.data.page
        })
        .catch(error => {
          console.log(error)
        })
    },

    makeLoan(id) {
      this.$confirm('确定放款吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return lendApi.makeLoan(id)
        })
        .then(response => {
          //放款成功则重新获取数据列表
          this.fetchData()
          this.$message({
            type: 'success',
            message: response.message
          })
        })
        .catch(error => {
          console.log('取消', error)
          if (error === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消放款'
            })
          }
        })
    },

    handleSizeChange(val) {
      this.pageDto.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageDto.pageIndex = val
      this.fetchData()
    }
  },
  mounted() {
    this.fetchData()
  }

}
</script>

<style scoped>

</style>
